package cn.zwxky.android.staff.Follow;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.Event.LsfbEvent;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInjectUtils;
import com.zgscwjm.lsfbbasetemplate.config.Config;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.SN;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.zwxky.android.staff.R;
import cn.zwxky.android.staff.Utils.URLString;

/**
 * Created by Administrator on 2017/3/29 0029.
 */

public class Arrange2 extends Fragment {
    /**
     * view对象
     */
    private View view;

    /**
     * activity对象
     */
    private Activity mactivity;

    /**
     * 点击刷新布局控件
     */
    @ViewInject(R.id.swiplayout)
    private SwipeRefreshLayout swipeRefreshLayout;

    /**
     * list列表
     */
    @ViewInject(R.id.dish_list)
    private RecyclerView dish_list;

    /**
     * RecyclerView的布局管理器
     */
    private RecyclerView.LayoutManager mlayoutManager;


    private List<NormalTaskBean.ListBean>  mList;
    private ArrangeAdapter adapter;

    private int log=0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_arrange, null);
        ViewInjectUtils.inject(this, view);


        mList=new ArrayList<>();
        adapter=new ArrangeAdapter(mactivity,mList,R.layout.item_arrange);
        mlayoutManager = new LinearLayoutManager(mactivity);//布局模式为listview方式
        dish_list.setLayoutManager(mlayoutManager);
        dish_list.setAdapter(adapter);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                log=1;
                getData();
            }
        });

        getData();
        return view;
    }

    /**
     * 用于存储activity对象
     *
     * @param activity
     */
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mactivity = activity;
    }

    /**
     * 获取数据
     */
    public void getData(){
        HashMap<String, String> hashMap = new HashMap<String, String>();
          hashMap.put("uid", Config.getUserId());
          hashMap.put("admin","0");
        new BaseInternet().getData_Get(URLString.NORMAL_TASK, hashMap, NormalTaskBean.class, new NormalTaskEvent3<NormalTaskBean>(), true,true);
    }

    @EventAnnotation
    public void NormalTaskEvent3(NormalTaskEvent3<NormalTaskBean> event){
        swipeRefreshLayout.setRefreshing(false);
        switch (event.getCode()){
            case 200:
                mList.clear();
                mList.addAll(event.getData().getList());
                adapter.notifyDataSetChanged();
                break;
            default:
                mList.clear();
                adapter.notifyDataSetChanged();
                if (log==1) {
                    SN.SnackShow(dish_list, event.getMsg());
                }
                log=0;
                break;
        }
    }

    @EventAnnotation
    public void ArrangeClickEvent(ArrangeClickEvent event){
             startActivity(new Intent(mactivity,ArrangeFollowActivity2.class).putExtra("id",event.getData()));
    }



    @Override
    public void onResume() {
        super.onResume();
        getData();
        LsfbEvent.getInstantiation().register2(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        LsfbEvent.getInstantiation().unregister2(this);
    }
}
